#include <iostream>
#include "stdTcpServer.h"
#include <unistd.h>
#include <json-c/json.h>
#include "stdShared.h"
#include <cstring>

using namespace std;

// 服务端口
#define SERVER_PORT 8080
#define USERINFO_SIZE 20
#define BUFFER_SIZE 128

void signUpLoginInterFace()
{
    cout << "1、注册" << endl;
    cout << "2、登录" << endl;
}

// 定义用户信息结构体
struct userInfo
{
    int type; // 1：注册  2：登录
    char name[USERINFO_SIZE];
    char passwd[USERINFO_SIZE];
};

// 注册信息
void registerInfo(StdTcpSocket *client)
{
    string username;
    cout << "请输入你的姓名：" << endl;
    cin >> username;

    string passwd;
    cout << "请输入你的密码：" << endl;
    cin >> passwd;

    // {}：json对象

    // 1、创建json对象
    json_object *jsonObj = json_object_new_object();

    // 2、向json对象添加<key, value>键值对
    json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(passwd.c_str()));

    // 3、将json对象转换成json字符串
    const char *jsonStr = json_object_to_json_string(jsonObj);
    cout << "jsonStr:" << jsonStr << endl;

    // 4、发送到服务器
    client->sendMessage(jsonStr);
}

// 登录信息
int loginInfo(StdTcpSocket *client)
{
    string username;
    cout << "请输入你的姓名：" << endl;
    cin >> username;

    string passwd;
    cout << "请输入你的密码：" << endl;
    cin >> passwd;

    // 1、创建json对象
    json_object *jsonObj = json_object_new_object();

    // 2、向json对象添加<key, value>键值对
    json_object_object_add(jsonObj, "type", json_object_new_int(LOGIN));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(passwd.c_str()));

    // 3、将json对象转换成json字符串
    const char *jsonStr = json_object_to_json_string(jsonObj);
    // cout << "jsonStr:" << jsonStr << endl;

    // 4、发送到服务器
    client->sendMessage(jsonStr);

    // 5、释放json对象
    json_object_put(jsonObj);

    // 6、接收服务器信息
    string resInfo;
    client->recvMessage(resInfo);
    // 将json字符串转为json对象
    json_object *resObj = json_tokener_parse(resInfo.c_str());

    int status = json_object_get_int(json_object_object_get(resObj, "status"));
    // 7、释放json对象
    json_object_put(resObj);

    if (status == LOGIN_SUCCESS)
    {
        cout << "登陆成功..." << endl;
        return true;
    }
    else
    {
        cout << "登陆失败..." << endl;
        return false;
    }
}

int main()
{
    StdTcpSocket client;
    client.connectToServer("192.168.27.129", SERVER_PORT);

    if (client.isConnected() == false)
    {
        std::cout << "连接服务器失败" << std::endl;
        exit(-1);
    }

    // while (1)
    // {
    //     client.sendMessage("hello world");
    //     sleep(1);
    // }

    signUpLoginInterFace();

    // 客户端给服务器发信息
    // string str;
    // char readBuffer[BUFFER_SIZE];

    int choice = 0;
    int ret = 0;
    while (1)
    {
        // 清空缓冲区
        // memset(readBuffer, 0, sizeof(readBuffer));

        cout << "请选择你想要进行的操作：";
        cin >> choice;

        switch (choice)
        {
        case 1:
            registerInfo(&client);
            break;
        case 2:
            ret = loginInfo(&client);
            if (ret == true)
            {
                // 跳转到新界面

            }

            break;
        default:
            break;
        }
    }

    return 0;
}